//
//  HMStatusCell.swift
//  WymanWeibo
//
//  Created by wyman on 16/4/14.
//  Copyright © 2016年 wyman. All rights reserved.
//

import UIKit

import SnapKit
class HMStatusCell: UITableViewCell {

    var bottomViewTopCons: Constraint?
    var status: HMStatus?{
        didSet{
            
        //在这里面传递给顶部View
        originalView.status = status
            
            bottomViewTopCons?.uninstall()
            //如果有转发的微博
            if let retweetStaus = status?.retweeted_status{
            retweetView.hidden = false
                retweetView.retweetStatus = retweetStaus
                
                bottomView.snp_updateConstraints(closure: { (make) in
                    
                    bottomViewTopCons = make.top.equalTo(retweetView.snp_bottom).constraint
                })
            
            
            }else{
            retweetView.hidden = true
            bottomView.snp_updateConstraints(closure: { (make) in
              bottomViewTopCons = make.top.equalTo(originalView.snp_bottom).constraint

            })
                
            
            }
            
            
        }
    }
    
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //构造函数里面搞事情
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
    //设置背景的颜色
        
        backgroundColor = UIColor(white: 237.0/255, alpha: 1)
        
        //准备UI
        prepareUI()
        
        
        
    }
    
    


    private func prepareUI(){
    //添加子控件
        
        contentView.addSubview(originalView)
        contentView.addSubview(bottomView)
        contentView.addSubview(retweetView)

  
        originalView.snp_makeConstraints { (make) -> Void in
            make.leading.right.equalTo(contentView)
            
            // 顶部有10得间距
            make.top.equalTo(contentView).offset(10)
            
            // 子控件的高度随机
            // 高度参照子控件的高度
            //            make.height.equalTo(Int(arc4random_uniform(200)) + 50)
        }
        
        //被转发的View
        
        retweetView.snp_makeConstraints { (make) in
            make.top.equalTo(originalView.snp_bottom)
            make.leading.trailing.equalTo(contentView)
        }
        
        // bottomView
        bottomView.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.equalTo(contentView)
//            make.top.equalTo(retweetView.snp_bottom)
            make.height.equalTo(37)
            
            //为了卸载它
            bottomViewTopCons = make.top.equalTo(retweetView.snp_bottom).constraint
        }
        
        // cell的高度根据里面子控件的约束来自动调整
        // 3.cell的子控件约束要正确,还需要约束contentView的约束(最主要的是高度约束),系统会让cell取contentView的高度
        contentView.snp_makeConstraints { (make) -> Void in
            // contentView的左右上参照cell
            make.leading.top.trailing.equalTo(self)
            
            // contentView的高度约束不能参照cell,需要参照子控件
            make.bottom.equalTo(bottomView)
        }
       
       //图层异步绘制
        self.layer.drawsAsynchronously = true
        
        //栅格化
        self.layer.shouldRasterize = true
        self.layer.rasterizationScale = UIScreen.mainScreen().scale
        

    
    }
    
    //懒加载 顶部的View
    private lazy var  originalView: HMStatusOriginalView = HMStatusOriginalView()

    
    private lazy var bottomView: HMStatusBottomView = HMStatusBottomView()
    
    //被转发的view
    private lazy var retweetView: HMStatusReetweet = HMStatusReetweet()

}
